layui.define(['layer', 'common', 'http'], function (exports) {

    var $ = layui.jquery
        , layer = layui.layer
        , setter = layui.setter
        , http = layui.http
        , common = layui.common;
    var utils = {
        /**
         * 加载字典列表
         * @param url 请求地址
         * @param dictValue 字典值
         * @param localKey 本地存储key
         * @param cb 回调函数
         */
        loadDictDataList: function (url, dictValue, localKey, cb) {
            var data = layui.data(setter.tableName, {key: setter.request[localKey]});
            if (!data) {
                http.get({
                    url: url
                    , data: {value: dictValue}
                    , success: function (res) {
                        layui.data(setter.tableName, {key: setter.request[localKey], value: res.data});
                        cb && cb(res.data);
                    }
                });
            } else {
                cb && cb(data);
            }
        },
        countAge: function (birthday) {
            var date = new Date();
            var startDate = new Date(birthday);
            var newDate = date.getTime() - startDate.getTime();
            var age = Math.ceil(newDate / 1000 / 60 / 60 / 24 / 365);
            if (isNaN(age)) {
                age = "";
            }
            return age;
        },
        initNormalCheckboxValue: function (e, data, fix) {
            if (data) {
                var _array = data.split(fix);
                $.each(_array, function (i, item) {
                    utils.initCheckboxTextValue(e, item);
                });
            }
        },
        initCheckboxTextValue: function (e, data) {
            if (data.indexOf(':')) {
                var textItemArray = data.split(':');
                var textItem = $(e + " :checkbox[value='" + textItemArray[0] + "']");
                textItem.prop("checked", true);
                textItem.next().next().find('[type=text]').val(textItemArray[1]);
                textItem.next().next().find('[type=text]').removeAttr('disabled');
            } else {
                $(e + " :checkbox[value='" + data + "']").prop("checked", true);
            }
        },
        initLabelRadioValue: function (e, data, fix) {
            var arr = [];
            if (data) {
                arr = data.split(fix);
            }
            if (arr && arr.length > 0) {
                $.each(arr, function (i, item) {
                    var str = item.split(':');
                    $(e + ' :radio[name=' + str[0] + '][value=' + str[1] + ']').prop('checked', true);
                })
            }
        },
        initLabelCheckboxValue: function (e, data, fix, subFix) {
            var arr = [];
            if (data) {
                arr = data.split(fix);
            }
            if (arr && arr.length > 0) {
                $.each(arr, function (j, it) {
                    var crf = it.split(subFix);
                    var el = $(e + '[data-label-name=' + crf[0] + ']');
                    $.each(crf, function (n, t) {
                        $(el).find(':checkbox[value=' + t + ']').prop("checked", true);
                    })
                });
            }
        }
    };

    exports('utils', utils);
});